# coding: utf-8
from PySide6.QtCore import Qt
from PySide6.QtGui import QBrush, QColor
from PySide6.QtWidgets import QWidget, QHBoxLayout, QVBoxLayout, QTableWidget, QAbstractItemView, QHeaderView, \
    QTableWidgetItem
from qfluentwidgets import TitleLabel, BodyLabel, setFont, themeColor

from components import CentreImageLabel, NewTableWidget


class MusicInfoWidget(QWidget):
    def __init__(self, pixmap: str, title: str, body: str, parent=None):
        super().__init__(parent)
        self.hBoxLayout = QHBoxLayout(self)
        self.vBoxLayout = QVBoxLayout()
        self.coverLabel = CentreImageLabel(pixmap, self)
        self.titleLabel = BodyLabel(title, self)
        self.bodyLabel = BodyLabel(body, self)

        self.coverLabel.setFixedSize(60, 60)
        self.titleLabel.setObjectName('titleLabel')
        self.bodyLabel.setObjectName('bodyLabel')
        setFont(self.titleLabel, 18)

        self.titleLabel.setTextColor(themeColor(), themeColor())
        self.bodyLabel.setTextColor(themeColor(), themeColor())

        self.titleLabel.setAlignment(Qt.AlignmentFlag.AlignLeft)
        self.bodyLabel.setAlignment(Qt.AlignmentFlag.AlignLeft)
        self.setFixedHeight(80)
        self.coverLabel.setBorderRadius(5, 5, 5, 5)
        self.hBoxLayout.setContentsMargins(0, 0, 0, 0)
        self.vBoxLayout.setContentsMargins(0, 0, 0, 0)

        self.hBoxLayout.setAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignCenter)
        self.vBoxLayout.setAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignCenter)
        self.vBoxLayout.addWidget(self.titleLabel, 0, Qt.AlignmentFlag.AlignLeft)
        self.vBoxLayout.addWidget(self.bodyLabel, 0, Qt.AlignmentFlag.AlignLeft)
        self.hBoxLayout.addWidget(self.coverLabel, 0, Qt.AlignmentFlag.AlignLeft)
        self.hBoxLayout.addLayout(self.vBoxLayout, 0)


class MusicListView(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.verticalLayout = QVBoxLayout(self)
        self.titleLabel = TitleLabel('标题', self)
        self.tableWidget = NewTableWidget(self)
        self.__initWidgets()

    def __initWidgets(self):
        self.tableWidget.verticalHeader().setVisible(False)
        self.tableWidget.setFrameShape(QTableWidget.Shape.NoFrame)
        self.tableWidget.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers)
        self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Stretch)
        self.tableWidget.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
        self.tableWidget.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection)
        self.tableWidget.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows)
        self.tableWidget.setShowGrid(False)

        self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.verticalLayout.setContentsMargins(10, 0, 0, 10)
        self.tableWidget.setViewportMargins(0, 0, 10, 0)
        self.verticalLayout.addWidget(self.titleLabel, 0, Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignLeft)
        self.verticalLayout.addWidget(self.tableWidget, 1)

    def addLeaderboardInfo(self, pixmap: str, title: str, body: str, volume: str, singer: str, duration: str):
        row = self.insertRow()
        widget = MusicInfoWidget(pixmap, title, body, self.tableWidget)
        self.setTableCellWidget(widget, row, 0)
        self.setTableItem(volume, row, 1)
        self.setTableItem(singer, row, 2)
        self.setTableItem(duration, row, 3)

    def setTableItem(self, text: str, row: int, col: int, color: QColor = QColor(themeColor())):
        item = QTableWidgetItem(text)
        item.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
        item.setForeground(QBrush(color))
        self.tableWidget.setItem(row, col, item)

    def setTableCellWidget(self, widget: QWidget, row: int, col: int):
        self.tableWidget.setCellWidget(row, col, widget)

    def insertRow(self, row: int = None, height: int = 80):
        if row is None:
            _row = self.tableWidget.rowCount()
            self.tableWidget.insertRow(_row)
            self.tableWidget.setRowHeight(_row, height)
            return _row
        else:
            self.tableWidget.insertRow(row)
            self.tableWidget.setRowHeight(row, height)
            return row

    def setTitle(self, title: str):
        self.titleLabel.setText(title)

    def setHorizontalHeaderLabels(self, labels: list):
        self.tableWidget.setColumnCount(len(labels))
        self.tableWidget.setHorizontalHeaderLabels(labels)
